package com.ruoyi.tj.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PyUtils {

    private static final char[] numbers = new char[]{ 'l','y','e','s','s','w','l','q','b','j' };

    static Character getFirstLetter(char c, HanyuPinyinOutputFormat outFormat) {
        String[] output = new String[0];
        try {
            output = PinyinHelper.toHanyuPinyinStringArray(c, outFormat);
            return output[0].charAt(0);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String getAllFirstLetter(String original) {
        char[] chars = original.toCharArray();
        StringBuilder sb = new StringBuilder();
        Character resultChar = null;

        HanyuPinyinOutputFormat outFormat = new HanyuPinyinOutputFormat();
        outFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        outFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for(char c : chars) {
            if(Character.isDigit(c)) {
                resultChar = numbers[Integer.parseInt(String.valueOf(c))];
            }
            else if(c > 128) {
                resultChar = getFirstLetter(c, outFormat);
            } else {
                resultChar = c;
            }
            sb.append(resultChar);
        }
        return sb.toString();
    }
}
